import axios from "axios";
import Cookie from "react-cookies";
import _ from "lodash";

//
// uyq ->
// http://u.y.qq.com
//
// cyq ->
// http://c.y.qq.com


async function setQCookies(cookies,f){
    await axios.get('/qqapi/user/setCookie?data='+cookies,{
        headers:{
            'content-type' : 'application/json'
        }
    }).then((r) => {
        //console.log(r)
    })
}
async function getQHotPlayList(f){


    await axios.get('/qqapi/recommend/playlist/u').then((r) => {
        let result = []
        //console.log(r)
        for (let playlist of r.data.data.list) {
            result.push(dealqResult(playlist))
        }
        //console.log("@@@q",result.length)

        f(result)
    })
}
function dealqResult(data){
    //console.log(data)
    let {title:name,content_id:id,cover:imgUrl,username : auth} = data;



    return {
        "name":name,
        "id":id,
        "imgUrl":imgUrl,
        "auth":auth,
        "from":'qq'
    }
}
async function getQPlayListDetail(id,f){
    await axios.get('/qqapi/songlist?id='+id).then((r) => {

        //console.log(r)
        let result = dealDetailResult(r.data.data)

        f(result)
    })
}
function dealDetailResult(data){
    //console.log(data)
    let {logo:img,visitnum:playCount,dissname:name,disstid:id,desc:description,nick:user} = data;
    let subscribedCount = ""

    let tags = data.tags.map((i) => {
        return i.name
    })
    let tracks = data.songlist;

    let r = tracks.map((i) => {
        let ts = i.interval;

        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }


        return{
            'times' : `${min} : ${sec}`,
            'name' : i.songname,
            'singer' : i.singer[0].name,
            'album' : i.albumname,
            'albumId' : i.albummid,
            'id' : i.songmid
        }
    })


    return {
        user:user,
        tags:tags,
        name:name,
        id:id,
        description:description,
        img:img,
        playCount:playCount,
        subscribedCount:subscribedCount,
        list:r
    }
}


async function getQPlayMusic(id,albumId,f){
    let resource = "";
    await axios.get(`/qqapi/song/urls?ownCookie=1&id=${id}`,).then((r) => {

        //console.log(r)
        let result = dealPlayMusicResultU(r.data.data)

        resource = result;
    })
    let img = ""
    await axios.get(`/qqapi/album?albummid=${albumId}`,).then((r) => {

        //console.log(r)
        let result = dealPlayMusicResult(r.data.data)
        img = result
    })
    if(resource === ""){
        f(-1)
    }else {
        f({resource: resource, img: img})
    }
}
function dealPlayMusicResultU(data){
    //console.log(data)
    let key = Object.values(data)[0]
    //console.log(key)
    return key || ""


}
function dealPlayMusicResult(data){

    return data?.picurl || "";


}
async function getQPlayStatus(id){
    return await axios.get('/qqapi/user/detail?id='+id)

}
async function getQSearch(keywords,pageNo,pageSize,type,f){
    await axios.get(`/qqapi/search?key=${keywords}&pageSize=${pageSize}&pageNo=${pageNo}&t=${type}`).then((r) => {

        //console.log(r)
        let result;
        if(type === 0 || type === '0'){
            result = dealSearchSongResult(r.data.data)
            let count = r.data.data?.total || 0
            result = {v:result,count:count}
        }else if(type === 9 || type === '9'){
            result = dealSearchSingerResult(r.data.data)
        }else if(type === 2 || type === '2'){
            result = dealSearchPlayListResult(r.data.data)
        }else if(type === 8 || type === '8'){
            result = dealSearchAlbumResult(r.data.data)
        }


        f(result)
    })
}
function dealSearchAlbumResult(data){
    //console.log(data)
    let als = data.list || []
    return als.map((r) => {

        return {
            "name":r.albumName,
            "id":r.albumMID,
            "img":r.albumPic,
            "auth":r.singerName,
            "from":'qq'
        }

    })



}
function dealSearchPlayListResult(data){
    //console.log(data)
    let als = data.list || []
    return als.map((r) => {

        return {
            "name":r.dissname,
            "id":r.dissid,
            "imgUrl":r.imgurl,
            "auth":r.creator.name,
            "from":'qq'
        }

    })

}
function dealSearchSongResult(data) {
    let tracks = data.list;

    let r = tracks.map((i) => {
        let ts = i.interval;

        let min = Math.floor(ts / 60);
        let sec = Math.floor(ts - min * 60);
        if (min < 10) {
            min = '0' + min;
        }
        if (sec < 10) {
            sec = '0' + sec;
        }


        return {
            'times': `${min} : ${sec}`,
            'name': i.songname,
            'singer': i.singer[0].name,
            'album': i.albumname,
            'albumId': i.albummid,
            'id': i.songmid,
            'from' : 'qq'
        }
    })
    return r
}
function dealSearchSingerResult(data) {
    //console.log(data)
    let result = data.list || {artistCount:0,artists:[{}]}
    let map = result.map((r) => {

        return {
            id:r.singerMID,
            name: r.singerName,
            img: r.singerPic,
            from:'qq'
        }
    });
    return map

}
async function getQSingerDetail(id,f){

    await axios.get(`/qqapi/singer/album?singermid=${id}&pageSize=100`).then((r) => {
        let result = dealQSingerDetailResult(r.data.data)
        //console.log(result)
        f(result)
    })

}

function dealQSingerDetailResult(data) {
    //console.log(data)
    let als = data.list || []

    let result = als.map((r) => {
        return{
            img:"/listico.png",
            id:r.album_mid,
            name:r.album_name,
            auth:r.singer_name,
            from:'qq'
        }
    });

    return result;



}
async function getQAlbumDetail(id,f){
    await axios.get('/qqapi/album/songs?albummid='+id).then((r) => {

        //console.log(r.data)
        let result = dealAlbumDetailResult(r.data)
        //console.log(result)
        f(result)
    })
}
function dealAlbumDetailResult(data){

    let tracks = data.data.list || [];

    let r = tracks.map((i) => {
        let ts = i.interval;

        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        return{
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.singer[0].name,
            'album' : i.album.name,
            'albumId' : i.album.mid,
            'id' : i.mid
        }
    })


    return r
}


async function getQCatList(f){
    await axios.get('/qqapi/songlist/category').then((r) => {

        //console.log(r.data)
        let result = dealCatListResult(r.data)
        //console.log(result)
        f(result)
    })
}

function dealCatListResult(data){
    let vs = data.data || []
    return vs.map((r) => {
        return{
            'name':r.type,
            'v':r.list
        }
    })

}
async function getQCatPlayList(cat,offset,limit,f){
    await axios.get(`/qqapi/songlist/list?pageSize=${limit}&pageNo=${offset}&category=${cat}`).then((r) => {

        //console.log(r.data)
        let result = dealCatPlayListResult(r.data)
        //console.log(result)
        let count = r.data.data.total
        f({v:result,count:count})
    })
}

function dealCatPlayListResult(data){
    let playlists = data.data.list || []
    //console.log(playlists)
    return playlists.map((r) => {

        return {
            "name":r.dissname,
            "id":r.dissid,
            "imgUrl":r.imgurl,
            "auth":r.creator.name,
            "from":'qq'
        }

    })

}
async function getQRank(f){
    await axios.get(`/qqapi/top/category`).then((r) => {

        //console.log(r.data)
        let result = dealRankResult(r.data)

        f(result)
    })
}
function dealRankResult(data){
    let list = data.data || []
    let r = list.map((i) => {

        return{
            name:i.title,
            list:i.list.map((r) => {
                return{
                    "name":r.label,
                    "id":r.topId,
                    "imgUrl":r.picUrl,
                    "auth":r.label,
                    "from":'qq',
                }
            }),

        }
    })
    return r;
}

async function getQRankMusicList(id,pageNo,pageSize,f){
    await axios.get(`/qqapi/top?id=${id}&pageNo=1&pageSize=100`).then((r) => {

        //console.log(r.data)
        let result = dealRankMusicListResult(r.data)

        f({v:result,count:100})
    })
}
function dealRankMusicListResult(data){
    let list = data.data?.list || []
    let r = list.map((i) => {
        let ts = i.interval;

        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        return{
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.singer[0].name,
            'album' : i.album.name,
            'albumId' : i.album.mid,
            'id' : i.mid
        }
    })
    return r;
}

async function getQSingerCats(f){
    await axios.get(`/qqapi/singer/category`).then((r) => {

        //console.log(r.data)
        let result = dealSingerCatsResult(r.data)

        f(result)
    })
}
function dealSingerCatsResult(data){

    return data.data;
}
async function getQSingerByCats(data,page,f){
    await axios.get(`/qqapi/singer/list?area=${data.area}&genre=${data.genre}&index=${data.index}&sex=${data.sex}&pageNo=${page}`).then((r) => {

        //console.log(r.data)
        let result = dealSingerByCatsResult(r.data)
        let count = r.data.data?.total
        f({v:result,count:count})
    })
}
function dealSingerByCatsResult(data){
    let list = data.data?.list || []
    return list.map((r) => {
        return {
            id:r.singer_mid,
            name: r.singer_name,
            img: r.singer_pic,
            from:'qq'
        }
    })

}
async function getQlyric(id,f){
    await axios.get(`/qqapi/lyric?songmid=${id}`).then((r) => {
        //console.log(r.data)
        let result = dealQlyricResult(r.data)

        f(result)
    })
}
function dealQlyricResult(data){

    return data.data.lyric || ''

}
export {getQHotPlayList,getQPlayListDetail,getQPlayMusic,setQCookies,getQPlayStatus
    ,getQSearch,getQSingerDetail,getQAlbumDetail,getQCatPlayList,getQCatList,getQRank,
    getQRankMusicList,getQSingerCats,getQSingerByCats,getQlyric

}
